package com.my.ext.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class AESCoder {

    private static Log LOGGER = LogFactory.getLog(AESCoder.class);
    
	public static class AESCBCUtil {
	    private static final String ENCODING = "utf-8";
	 
	    private static final String KEY_ALGORITHM = "AES";
	    
	    /**
	     * 加解密算法/工作模式/填充方式
	     */
	    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
	    
	    /**
	     * 填充向量
	     */
//	    public static final String FILL_VECTOR = "1234560405060708";
	 
//	    private static final SecretKeySpec PASSWORD;
//	    static {
//	    	byte[] raw = "plusReadEase.52#".getBytes();
//	    	PASSWORD = new SecretKeySpec(raw, KEY_ALGORITHM);
//	    }
	    
	    public static void genAES() {
	    	
	    }
	    
	    /**
	     * 加密字符串
	     *
	     * @param content  字符串
	     * @return
	     * @throws Exception
	     */
//	    public static String encrypt(String content,byte[] ivs) {
//	        if (null == content || content.trim().isEmpty()) {
//	            LOGGER.error("AES encryption params is null");
//	            return null;
//	        }
//	 
//	        Cipher cipher = null;
//	        try {
//	            cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
//	            IvParameterSpec iv = new IvParameterSpec(ivs);
//	            cipher.init(Cipher.ENCRYPT_MODE, PASSWORD, iv);
//	            
//	            byte[] anslBytes = content.getBytes(ENCODING);
//	            byte[] encrypted = cipher.doFinal(anslBytes);
//	            return Base64.encodeBase64URLSafeString(encrypted);
//	        } catch (Exception e) {
//	        	e.printStackTrace();
//	            LOGGER.error("AES encryption operation has exception,content:"+content, e);
//	        }
//	        return null;
//	    }
	 
	    /**
	     * 解密
	     *
	     * @param content  解密前的字符串
	     * @return
	     * @throws Exception
	     */
	    public static String decrypt(String content,byte[] ivs, byte[] pwd) {
	        if (null == content || content.trim().isEmpty()) {
	            LOGGER.error("AES encryption params is null");
	            return null;
	        }
	        SecretKeySpec PASSWORD = new SecretKeySpec(pwd, KEY_ALGORITHM);
	        try {
	            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
	            IvParameterSpec iv = new IvParameterSpec(ivs);
	            cipher.init(Cipher.DECRYPT_MODE, PASSWORD, iv);
	            
	            byte[] encrypted1 = Base64.decodeBase64(content);
	            byte[] original = cipher.doFinal(encrypted1);
	            return new String(original, ENCODING);
	        } catch (Exception e) {
	            LOGGER.error("AES decryption operation has exception,content:"+content, e);
	        }
	        return null;
	    }
	 
//	    public static void main(String[] args) throws Exception {
////	        String str = "{\"orgCode\":\"hilib\",\"openId\":\"oNBAgxPlXxdCLuC6Zo3MQRIFM_JY\",\"accountId\":17165}";
//	        String str = "{\"orgCode\":\"qingkui\",\"time\":1609430400000}";
//	        //必须为16位
//	        //生成加密密钥
//	        String encryptStr = encrypt(str);
//	        System.out.println(encryptStr);
//	        
//	        String decryptStr = decrypt(encryptStr);
//	        System.out.println(decryptStr);
//	    }
	}
}
